/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;

import models.*;
import views.*;

/**
 *
 * @author dennis
 */
public class Menu {
    
    private MenuPanel menuPanel = new MenuPanel();
    private String[] levels;
    private Sokoban sokoban;
    private int steps;
    
    /**
     * Creates a new Menu.
     * @param sokoban object of Sokoban
     */
    public Menu(Sokoban sokoban){
        
        this.sokoban = sokoban;
        
        initStartActionListeners();  
        
        findAndSetLevels();
        defaultChildMode();
        
    }
    
    /**
     * Get the menuPanel
     * @return the menuPanel
     */
    public MenuPanel getMenuPanel(){
        return menuPanel;
    }
    
    /**
     * Every time the user makes a valid move, we increment the steps
     */
    public void incrementStep(){
        steps++;
        updateSteps();
    }

        /**
     * Every time the user undoes a move, we decrement the steps
     */
    public void decrementStep(){
        if(steps > 0){
            steps--;
            updateSteps();
        }
    }
    
    /**
     * This updates the view with the new steps amount
     */
    public void updateSteps(){
        menuPanel.updateSteps(this.steps);
    }
    
    public int getStep(){
        return this.steps;
    }
    
    /**
     * For example when we start a new level or restart a current one, set steps to 0l
     */
    public void resetSteps(){
        steps = 0;
        menuPanel.resetSteps();
    }
    
    /**
     * There's a folder containing all our levels. This method finds those levels and sets them to the dropdownmenu in het menuPanel
     */
    public void findAndSetLevels(){
        
        URL url = getClass().getResource("../levels");
        File directory = new File(url.getPath());
        File[] fList = directory.listFiles();
 
        levels = new String[directory.listFiles().length];

        int i = 0;
        for (File file : fList){
            if (file.isFile()){
                levels[i] = file.getName();
                i++;
            }
        }
        
        menuPanel.addLevelsToPicker(levels);
    }
    
    /**
     * Gets the selected level from the level dropdown in menuPanel
     * @return name of the selected level
     */
    public String getSelectedLevel(){
        return menuPanel.getSelectedLevel();
    }
    
    /**
     * Sets the process to the menuPanel
     * @param process 
     */
    public void setProgress(int process){
        menuPanel.setProgress(process);
    }
    
    /**
     * Sets the default childmode, based on the default selected childmode radiobutton
     */
    public void defaultChildMode(){
        menuPanel.defaultChildMode();
    }
    
    /**
     * Gets the selected childmode
     * @return the selected childmode setting, on or off
     */
    public String getSelectedChildMode(){
        return menuPanel.getSelectedChildMode();
    }
    
    /**
     * We override actionlisters so we can add some logic over here in stead of in the view
     */
    private void initStartActionListeners(){
        
        menuPanel.addStartActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
               sokoban.revalidateGamePanel(getSelectedLevel());
            }
        });
        
        menuPanel.addLevelPickerActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                //System.out.println(this);                
            }
        });
        
        menuPanel.addChildToggleActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                if(menuPanel.getSelectedChildMode() == "On"){
                    sokoban.game.setChildMode(true);
                } else {
                    sokoban.game.setChildMode(false);
                }
                sokoban.gamePanel.requestFocusInWindow();
            }
        });
        
        menuPanel.addUndoActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                sokoban.game.undoMove();               
            }
        });
        
       
    }
    
}
